package com.teamall.web.app.controller;

import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.util.SpringAppContextUtil;
import com.teamall.web.app.vo.AidObjVO;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.family.service.IFamilyMemberService;
import com.teamall.web.fpgz.db.po.CodeFamilyInfoPO;
import com.teamall.web.fpgz.service.ICodeFamilyInfoService;
import com.teamall.web.fpxm.db.po.AidProjectProgressPO;
import com.teamall.web.sys.db.po.SysUserPO;
import com.teamall.web.sys.db.po.SysUserTokenPO;
import com.teamall.web.sys.service.ISysUserService;
import com.teamall.web.sys.service.impl.SysUserTokenServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author
 * @create 2017-07-01 23:18
 **/
@Controller
@RequestMapping("/app/aidObj")
public class AidObjController {
    @Autowired
    private ICodeFamilyInfoService codeFamilyInfoService;
    @Autowired
    private ISysUserService sysUserService;
    @RequestMapping("/query")
    @ResponseBody
    public ResResult query(HttpServletRequest request,  Page<CodeFamilyInfoPO> resultPage) throws BaseAppException{
        ResResult resResult = new ResResult();
        String token = request.getHeader("token");
        Integer userInfoId = this.getUserByToken(token).getUserId();
        SysUserPO sysUserPO = sysUserService.selectByPrimaryKey(userInfoId);
        CodeFamilyInfoPO codeFamilyInfoPO = new CodeFamilyInfoPO();
        Integer userObjId = sysUserPO.getUserObjId();
        codeFamilyInfoPO.setAidMainStaff(userObjId);
        codeFamilyInfoService.selectByArgAndPage(codeFamilyInfoPO,resultPage);
        Page<AidObjVO> voPage = new Page<AidObjVO>();
        List<AidObjVO> voList = new ArrayList<AidObjVO>();
        BeanUtils.copyProperties(resultPage,voPage);
        for (CodeFamilyInfoPO po : resultPage.getResultList()){
            AidObjVO aidObjVO = new AidObjVO();
            BeanUtils.copyProperties(po,aidObjVO);
            aidObjVO.setFamilyMasterPhoto(po.getPersonPhoto());
            aidObjVO.setFamilyName(po.getPersonName());
            aidObjVO.setFamilMasetrTel(po.getPersonTel());
            aidObjVO.setUserId(po.getAidMainStaff());
            voList.add(aidObjVO);
        }
        voPage.setResultList(voList);
        resResult.setRspData(voPage);
        return resResult;
    }
    public SysUserTokenPO getUserByToken(String token) throws BaseAppException {
        SysUserTokenServiceImpl sysUserTokenService = SpringAppContextUtil.getBean(SysUserTokenServiceImpl.class);
        SysUserTokenPO sysUserTokenPO = sysUserTokenService.selectByToken(token);
        return sysUserTokenPO;
    }
}
